
<xsl:stylesheet xmlns:marc="http://www.loc.gov/MARC21/slim" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" exclude-result-prefixes="marc">
    <xsl:output method="xml" indent="yes" encoding="utf-8"/>
    <xsl:template match="/">
        <add>
            <xsl:call-template name="marc:record"/>
        </add>
    </xsl:template>
    <xsl:template name="marc:record">
        <xsl:for-each select="//marc:record[@status='deleted']">
            <doc>
                <field name="id">
                    <xsl:value-of select="marc:controlfield[@tag=001]"/>
                </field>
                <field name="format">
                    <xsl:value-of select="./marc:controlfield[@tag=007]"/>
                </field>
                <field name="language">
                    <xsl:value-of select="substring(./marc:controlfield[@tag=008], 36, 3)"/>
                </field>
                <xsl:if test="marc:datafield[@tag=020]/marc:subfield[@code='a']">
                    <field name="isbn">
                        <xsl:value-of select="marc:datafield[@tag=020]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=022]/marc:subfield[@code='a']">
                    <field name="issn">
                        <xsl:value-of select="marc:datafield[@tag=022]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=520]/marc:subfield[@code='a']">
                    <field name="annotace">
                        <xsl:value-of select="marc:datafield[@tag=520]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:choose>
                    <xsl:when test="marc:datafield[@tag=090]">
                        <field name="callnumber">
                            <xsl:value-of select="marc:datafield[@tag=090]/marc:subfield[@code='a']"/>
                            <xsl:value-of select="marc:datafield[@tag='090']/marc:subfield[@code='b']"/>
                        </field>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:if test="marc:datafield[@tag=050]">
                            <field name="callnumber">
                                <xsl:value-of select="marc:datafield[@tag=050]/marc:subfield[@code='a']"/>
                                <xsl:value-of select="marc:datafield[@tag='050']/marc:subfield[@code='b']"/>
                            </field>
                        </xsl:if>
                    </xsl:otherwise>
                </xsl:choose>
                <xsl:if test="marc:datafield[@tag=100]/marc:subfield[@code='a']">
                    <field name="author">
                        <xsl:value-of select="marc:datafield[@tag=100]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=110]/marc:subfield[@code='a']">
                    <field name="author">
                        <xsl:value-of select="marc:datafield[@tag=110]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=111]/marc:subfield[@code='a']">
                    <field name="author">
                        <xsl:value-of select="marc:datafield[@tag=111]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <field name="title">
                    <xsl:value-of select="marc:datafield[@tag=245]/marc:subfield[@code='a']"/>
                    <xsl:text></xsl:text>
                    <xsl:value-of select="marc:datafield[@tag='245']/marc:subfield[@code='b']"/>
                </field>
                <xsl:for-each select="marc:datafield[@tag=246]">
                    <field name="title2">
                        <xsl:value-of select="./marc:subfield[@code='a']"/>
                    </field>
                </xsl:for-each>
                <xsl:if test="marc:datafield[@tag=240]/marc:subfield[@code='a']">
                    <field name="title2">
                        <xsl:value-of select="marc:datafield[@tag=240]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=130]/marc:subfield[@code='a']">
                    <field name="title2">
                        <xsl:value-of select="marc:datafield[@tag=130]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=260]/marc:subfield[@code='b']">
                    <field name="publisher">
                        <xsl:value-of select="marc:datafield[@tag=260]/marc:subfield[@code='b']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=260]/marc:subfield[@code='c']">
                    <field name="publishDate">
                        <xsl:value-of select="marc:datafield[@tag=260]/marc:subfield[@code='c']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=300]/marc:subfield[@code='b']">
                    <field name="physical">
                        <xsl:value-of select="marc:datafield[@tag=300]/marc:subfield[@code='b']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=362]/marc:subfield[@code='a']">
                    <field name="dateSpan">
                        <xsl:value-of select="marc:datafield[@tag=362]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=440]/marc:subfield[@code='a']">
                    <field name="series">
                        <xsl:value-of select="marc:datafield[@tag=440]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=505]/marc:subfield[@code='a']">
                    <field name="contents">
                        <xsl:value-of select="marc:datafield[@tag=505]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=505]/marc:subfield[@code='t']">
                    <field name="contents">
                        <xsl:for-each select="marc:datafield[@tag=505]/marc:subfield[@code='t']">
                            <xsl:value-of select="."/>
                            <xsl:text></xsl:text>
                        </xsl:for-each>
                    </field>
                </xsl:if>
                <xsl:call-template name="subjects"/>
                <xsl:for-each select="marc:datafield[@tag=700]">
                    <field name="author2">
                        <xsl:value-of select="./marc:subfield[@code='a']"/>
                    </field>
                </xsl:for-each>
                <xsl:if test="marc:datafield[@tag=780]/marc:subfield[@code='a']">
                    <field name="oldTitle">
                        <xsl:value-of select="marc:datafield[@tag=780]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=785]/marc:subfield[@code='a']">
                    <field name="newTitle">
                        <xsl:value-of select="marc:datafield[@tag=785]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="marc:datafield[@tag=830]/marc:subfield[@code='a']">
                    <field name="series">
                        <xsl:value-of select="marc:datafield[@tag=830]/marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:for-each select="marc:datafield[@tag=856]/marc:subfield[@code='u']">
                    <field name="url">
                        <xsl:value-of select="."/>
                    </field>
                </xsl:for-each>
            </doc>
        </xsl:for-each>
    </xsl:template>
    <xsl:template name="subjects">
        <xsl:for-each select="marc:datafield[@tag=600]">
            <field name="subject1">
                <xsl:value-of select="./marc:subfield[@code='a']"/>
            </field>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag=610]">
            <field name="subject2">
                <xsl:value-of select="./marc:subfield[@code='a']"/>
            </field>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag=630]">
            <field name="subject3">
                <xsl:value-of select="./marc:subfield[@code='a']"/>
            </field>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag=650]">
            <field name="subject4a">
                <xsl:value-of select="./marc:subfield[@code='a']"/>
            </field>
            <field name="subject4x">
                <xsl:value-of select="./marc:subfield[@code='x']"/>
            </field>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag=651]">
            <field name="subject5">
                <xsl:value-of select="./marc:subfield[@code='a']"/>
            </field>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag=655]">
            <field name="subject6">
                <xsl:value-of select="./marc:subfield[@code='a']"/>
            </field>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>